package demo1;

import java.util.ArrayList;
import java.util.List;

public class TreeNode {
    static class Node {
        public int val;
        public Node left;
        public Node right;

        public Node(int val) {
            this.val = val;
        }
    }
    Node root = null;
    //创建树
    public Node createTree() {
        Node n1 = new Node(11);
        Node n2 = new Node(21);
        Node n3 = new Node(31);
        Node n4 = new Node(41);
        Node n5 = new Node(51);
        Node n6 = new Node(61);
        n1.left = n2;
        n1.right = n3;
        n2.left = n4;
        n2.right = n5;
        n3.right = n6;
        return n1;
    }
    List<Integer> list = new ArrayList<>();
    public List<Integer> postorderTraversal(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        List<Integer> left = postorderTraversal(root.left);
        list.addAll(left);
        List<Integer> right = postorderTraversal(root.right);
        list.addAll(right);
        list.add(root.val);
        return list;
    }
    public void postorder(Node root) {
        if (root == null) {
            return;
        }
        postorder(root.left);
        postorder(root.right);
        list.add(root.val);
    }

    public List<Integer> inorderTraversal(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        List<Integer> left = inorderTraversal(root.left);
        list.addAll(left);
        list.add(root.val);
        List<Integer> right = inorderTraversal(root.right);
        list.addAll(right);
        return list;
    }
    public void inorder(Node root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        list.add(root.val);
        inorder(root.right);
    }



    public List<Integer> preorderTraversal(Node root) {
        List<Integer> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        list.add(root.val);
        List<Integer> left = preorderTraversal(root.left);
        list.addAll(left);
        List<Integer> right = preorderTraversal(root.right);
        list.addAll(right);

        return list;
    }
    public void pre(Node root) {
        if (root == null) {
            return;
        }
        list.add(root.val);
        pre(root.left);
        pre(root.right);
    }





}
